package ruifeng.zcj.mybase.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;
import ruifeng.zcj.mybase.R;
import ruifeng.zcj.mybase.base.utils.TUtil;
/**
 * Created by Administrator on 2016/8/15 0015.
 */
public abstract class BaseActivity<T extends BasePresenter,E extends BaseModel> extends MySwipeBackActivity {
    public T mPresenter;
    public E mModel;
    public Context mContext;
//    public boolean isNight;
    private ImageView ivShadow;

    private SweetAlertDialog mLoadingDialog;

    protected App mApp;
    private FragmentConfig fragmentConfig;
    protected final static class FragmentConfig extends UIConfig {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setTheme(R.style.OrangeTheme);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        this.setContentView(this.getLayoutId());
        ButterKnife.bind(this);
        mApp = App.getInstance();


        mContext = this;
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        this.initView();
        if (this instanceof BaseView) mPresenter.setVM(this, mModel);


        fragmentConfig = new FragmentConfig();
        initUIConfig(fragmentConfig);
        if (fragmentConfig.isApplyEventBus) {
            EventBus.getDefault().register(this);
        }

    }
    protected void initUIConfig(FragmentConfig fragmentConfig) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.onDestroy();
        ButterKnife.unbind(this);

        if (fragmentConfig.isApplyEventBus) {
            EventBus.getDefault().unregister(this);
        }
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    public void reload(){
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }
    public void showSnackBar(String message){
        Toast.makeText(App.getAppContext(),""+message,Toast.LENGTH_SHORT).show();
//        SnackBar.make(App.getAppContext()).actionText(message).duration(Snackbar.LENGTH_SHORT).show();
    }
    public abstract int getLayoutId();

    public abstract void initView();


    /**
     * 查找View
     *
     * @param id   控件的id
     * @param <VT> View类型
     * @return
     */
    public  <VT extends View> VT getViewById(@IdRes int id) {
        return (VT) findViewById(id);
    }


    public void showLoadingDialog(String titleText) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
            mLoadingDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.colorPrimary));
            mLoadingDialog.setCancelable(false);
            mLoadingDialog.setTitleText(""+titleText);
        }
        mLoadingDialog.show();
    }

    public void dismissLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

}
